<template>
  <div class="employees">
    <!-- 顶部模块 -->
    <el-card class="top-card">
      <div class="btn">
        <el-button type="primary" @click="outputClick">导出</el-button>
        <el-button type="primary" @click="importClick">导入</el-button>
        <el-button type="primary" @click="addEvent">+ 新增员工</el-button>
      </div>
    </el-card>
    <!-- 内容处理 -->

    <el-card>
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" sortable prop="username" />
        <el-table-column label="头像" sortable prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-globalImgerror="defaultImg"
              class="headerimg"
              :src="row.staffPhoto"
              alt=""
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="工号" sortable prop="workNumber" />
        <el-table-column label="聘用形式" sortable prop="formOfEmployment" />
        <!-- 使用过滤器 -->
        <!-- <template v-slot="{ row }">
            <div>
              {{ row.formOfEmployment | formatterFormOf }}
            </div>
          </template> -->
        <el-table-column label="部门" sortable prop="departmentName" />
        <el-table-column label="入职时间" sortable prop="timeOfEntry" />
        <el-table-column label="状态" sortable prop="enableState" />
        <el-table-column label="操作" sortable width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="page">
      <el-pagination
        layout="total,sizes,prev,pager,next,jumper"
        :total="page.total"
        :page-size="page.size"
        :page-sizes="[1, 5, 10, 100, 200]"
        :current-page="page.page"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <Add ref="add" @getData="getData" />

    <!-- 图像弹框组件 -->
    <el-dialog title="头像" :visible.sync="show" width="500px">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sysUser, sysUserDel } from '@/api/employees.js'
import employessData from '@/api/constant/employees.js'
import Add from './components/add.vue'
import moment from 'moment'
import cookieJs from 'js-cookie'
import Qrcode from 'qrcode'
export default {
  components: {
    Add
  },
  data() {
    return {
      defaultImg: require('@/assets/common/head.jpg'),
      list: [{}],
      page: {
        total: 100,
        size: +cookieJs.get('size') || 5,
        page: 1
      },
      show: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取员工列表
    async getData() {
      const res = await sysUser(this.page)
      // 获取数据前，先将数据转换
      res.data.rows.forEach((item) => {
        const result = employessData.hireType.find((i) => {
          // 将数据内容转换为数字类型
          return i.id === +item.formOfEmployment
        })
        const resultStr = result ? result.value : '---'
        item.formOfEmployment = resultStr
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })

      this.list = res.data.rows
      this.page.total = res.data.total
      // console.log('员工列表', res)
    },
    sizeChange(size) {
      this.page.size = size
      cookieJs.set('size', size)
      this.page.page = 1
      this.getData
    },
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    // element 自带的数据转换
    // formatter(rwo, column, cellValue, index) {
    //   const result = employessData.hireType.find((item) => {
    //     return item.id === cellValue
    //   })
    //   return result ? result.value : '---'
    // }
    addEvent() {
      this.$refs.add.isShow = true
    },
    del(id) {
      this.$confirm('您确定删除该用户吗', '提示').then(async() => {
        await sysUserDel(id)
        this.$message.success('删除成功')
        this.pageChange(1)
      })
    },
    // 点击跳转导入
    importClick() {
      this.$router.push('/employees/import')
    },
    // 点击跳转到查看详情
    goDetail(id) {
      this.$router.push('/employees/detail/' + id)
    },
    // 导出exlce文件
    async outputClick() {
      const header = {
        姓名: 'username', //	string	非必须		姓名
        手机号: 'mobile', //	string	非必须		手机号
        聘用形式: 'formOfEmployment', //	number	非必须		聘用形式
        工号: 'workNumber', //	string	非必须		工号
        组织名称: 'departmentName', //	string	非必须		组织名称
        入职时间: 'timeOfEntry', //	string	非必须		入职时间
        转正时间: 'correctionTime' //	string	非必须		转正时间
      }

      const res = await sysUser({
        page: 1,
        size: 10000
      })

      res.data.rows.forEach((item) => {
        const result = employessData.hireType.find((i) => {
          // 将数据内容转换为数字类型
          return i.id === +item.formOfEmployment
        })
        const resultStr = result ? result.value : '---'
        item.formOfEmployment = resultStr
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })

      const data = res.data.rows.map((item) => {
        return Object.keys(header).map((item2) => {
          return item[header[item2]]
        })
      })
      import('@/vendor/Export2Excel')
        .then((res) => {
          res.export_json_to_excel({
            header: Object.keys(header),
            data,
            filename: '用户列表'
          })
        })
        .catch(() => {})
    },

    // 打开头像弹框组件
    imgClick(url) {
      if (!url) {
        return
      }
      this.show = true
      // 先等默认插槽标签渲染完成后再生成二维码
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, url, {
          width: 300,
          height: 300
        })
      })

      // console.log(this.list)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;
  .top-card {
    margin-bottom: 15px;
    .btn {
      text-align: right;
    }
  }
  .page {
    padding-top: 15px;
    text-align: right;
  }
  .headerimg {
    width: 70px;
    height: 70px;
  }
}
</style>
